package biz.bbtec.sbm.user.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import biz.bbtec.sbm.user.bean.UserBean;
import biz.bbtec.sbm.user.factory.UserFactory;
import biz.bbtec.sbm.user.service.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserLoginAction extends ActionSupport implements SessionAware {

	private UserBean user;
	private Map session;

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	@SuppressWarnings("unchecked")
	public String login() throws Exception {
		UserBean userbean = new UserBean();
		UserService userdao = UserFactory.getInstance().createUserService();
		userbean.setUser(user.getUser());
		userbean = userdao.Login(userbean);
		if (userbean != null
				&& userbean.getPass().equalsIgnoreCase(user.getPass())) {
			this.session.put("user", userbean);
			return SUCCESS;
		} else {
			this.addActionError("用户名或密码错误，请再次输入！");
			return INPUT;
		}
	}

	public String showUser() {
		Map session = ActionContext.getContext().getSession();
		UserBean userbean = (UserBean) session.get("user");
		UserService userdao = UserFactory.getInstance().createUserService();
		user = userdao.show(userbean);
		if (user == null) {
			this.addActionError("出错啦！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String modifyuser() {
		if (user.getName().trim().equalsIgnoreCase("")) {
			this.addActionError("请输入您的真实姓名！");
			return INPUT;
		}
		if (!(user.getPass().trim().equalsIgnoreCase(user.getRepass()))) {
			this.addActionError("两次密码输入不相同！");
			return INPUT;
		}
		UserService userdao = UserFactory.getInstance().createUserService();
		if (!(userdao.Modify(user))) {
			this.addActionError("修改失败！");
			return INPUT;
		} else {
			return SUCCESS;
		}
	}
}
